/*
 * Main.java
 *
 * Created on 26 de julio de 2006, 01:37 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package run;

import java.io.File;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import splas.Splas;
import util.LoadProperties;

/**
 *
 * @author jsanchez
 */
public class Main {

    public Splas splas = new Splas(new JFrame());
    /** Creates a new instance of Main */

    public Main() {
        new MainRun().start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main();
        //System.out.println(System.getProperties().get("java.home"));
    }

    public class MainRun extends Thread {

        @SuppressWarnings("empty-statement")
        @Override
        public void run() {
            String lib = LoadProperties.getProperties("conf.properties", "lib").trim();
            String run = LoadProperties.getProperties("conf.properties", "run").trim();
            String param = LoadProperties.getProperties("conf.properties", "param").trim();
            String ms = LoadProperties.getProperties("conf.properties", "ms").trim();
            String mx = LoadProperties.getProperties("conf.properties", "mx").trim();
            String tiempoEspera = LoadProperties.getProperties("conf.properties", "tiempoEspera").trim();
            String separador = System.getProperties().getProperty("path.separator");
            
            String JAVA_HOME=LoadProperties.getProperties("conf.properties", "JAVA_HOME").trim();
            while(JAVA_HOME==null || !new java.io.File(JAVA_HOME).exists()){
                String default_jdk=System.getProperties().get("java.home").toString();
                default_jdk=default_jdk!=null?default_jdk:"C:/";
                JAVA_HOME=JOptionPane.showInputDialog("Escriba aqui la ruta de instalación de java",default_jdk);
                if(JAVA_HOME!=null && new java.io.File(JAVA_HOME).exists()){                    ;
                    Properties p= LoadProperties.loadPropertiess("conf.properties");
                    p.put("JAVA_HOME", JAVA_HOME);
                    LoadProperties.storePropertiess(p,"conf.properties" , "Aplicacion");
                }                
            }
            
            mx = (mx == null || mx.trim().equals("")) ? "" : "-Xmx" + mx;
            ms = (ms == null || ms.trim().equals("")) ? "" : "-Xms" + ms;
            param = param == null ? "" : param;
            String PATH = "";
            File file = new File(lib);
            Integer valor = 1;
            Integer valorMaximo = 0;
            splas.setVisible(true);
            if (file.isDirectory()) {

//                URLClassLoader cargadorJVM = (URLClassLoader) ClassLoader.getSystemClassLoader();
                valorMaximo = file.listFiles().length + 1;
                splas.setValorMaxino(valorMaximo);                
                for (int i = 0; i < file.listFiles().length; i++) {
                    if (!PATH.trim().equals("")) {
                        PATH = PATH + separador;
                    }
                    try {
                        Thread.sleep(30);
                    } catch (Exception e) {
                    }
                    String archivoJar = file.listFiles()[i].getAbsolutePath();
                    String FILE = file.listFiles()[i].getPath();
                    if (archivoJar.trim().endsWith("jar") || archivoJar.trim().endsWith("zip")) {
                        PATH += FILE;
//                        Class claseJVM = URLClassLoader.class;
//                        Class params[] = new Class[]{URL.class};
//                        try {
                            File objFile = new File(archivoJar);
//                            Method metodo = claseJVM.getDeclaredMethod("addURL", params);
//                            //esta linea es para volver accesible el método
//                            metodo.setAccessible(true);
//                            URL objUrl = objFile.toURL();
//                            metodo.invoke(cargadorJVM, new Object[]{objUrl});
                            splas.setValor(valor++, objFile.getName());
//                        } catch (Exception th) {
//                            th.printStackTrace();
//                        }
                    }
                }
                try {
                    System.out.println("java -classpath " + PATH + " " + ms + " " + mx + " " + run + " " + param);
                    Runtime.getRuntime().exec(JAVA_HOME+"/bin/java -classpath " + PATH + " " + ms + " " + mx + " " + run + " " + param);
                    //                    Class classs=Class.forName(run.trim());
//                    Constructor[] constructors=classs.getConstructors();
//                    constructors[0].newInstance("tree");
                    
                    try {
                        Thread.sleep(Integer.valueOf(1000));
                    } catch (Exception e) {
                    }
                    splas.setValor(valorMaximo, run);
                    try {
                        Thread.sleep(Integer.valueOf(tiempoEspera));
                    } catch (Exception e) {
                    }
                    splas.setVisible(false);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }finally{
                    
                }
            } 
            System.exit(1);
        }
    }
    
    //TEST
    
}
